package site.marqstree.kotlinmall.ui.fragment.home

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bigkoo.convenientbanner.listener.OnItemClickListener
import io.reactivex.rxkotlin.subscribeBy
import kotlinx.android.synthetic.main.fragment_home.*
import me.crosswall.lib.coverflow.CoverFlow
import org.jetbrains.anko.support.v4.startActivity
import site.marqstree.kotlin.library.base.data.net.request.RxRequest
import site.marqstree.kotlin.library.base.extent.json2List
import site.marqstree.kotlin.library.base.data.net.observer.RxObserver
import site.marqstree.kotlin.library.base.ui.fragment.BaseFragment
import site.marqstree.kotlin.library.base.ui.widget.banner.ImageBannerAdapter
import site.marqstree.kotlin.library.base.ui.widget.newsflipper.News
import site.marqstree.kotlin.library.base.util.callback.CallbackManager
import site.marqstree.kotlin.library.base.util.callback.CallbackType
import site.marqstree.kotlin.library.base.util.callback.IGlobalCallback
import site.marqstree.kotlin.library.good.ui.activity.goodlist.ActivityGoodList
import site.marqstree.kotlinmall.R
import site.marqstree.kotlinmall.constant.Constant
import site.marqstree.kotlinmall.ui.fragment.home.discount.DiscountAdapter
import site.marqstree.kotlinmall.ui.fragment.home.discount.DiscountData
import site.marqstree.kotlinmall.ui.fragment.home.topic.TopicAdapter
import java.util.*


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlinmall.ui.fragment
 * 文件名: HomeFragment
 * 创建者: marqstree
 * 创建时间: 2020/2/24 16:57
 * 描述: TODO
 */
/*
    主界面Fragment
 */
class HomeFragment: BaseFragment(), OnItemClickListener {

    private lateinit var discountAdapter:DiscountAdapter
    private lateinit var mBannerAdapter: ImageBannerAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val rootView = inflater.inflate(R.layout.fragment_home,null)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initBanner()
        initNews()
        initDiscount()
        initTopic()
        initSearch()
    }

    private fun initSearch() {
        CallbackManager.instance
            .addCallback(CallbackType.SEARCH_HISTORY,
                object :IGlobalCallback<String>{
                override fun executeCallback(arg: String) {
                    startActivity<ActivityGoodList>(
                        "mode" to ActivityGoodList.MODE_SEARCH_KEYWORD,
                        "serachkeyword" to arg)
                }
            })
    }

    private fun initBanner() {
        RxRequest.builder()
            .setContext(context!!)
            .setUrl(Constant.BANNERS_URL)
            .get()
            .json2List(String::class.java)
            .subscribe(object :
                RxObserver<List<String>>(){
                override fun onNext(list: List<String>) {
                    mBannerAdapter = ImageBannerAdapter(list, activity as Context)
                    mHomeBanner.adapter = mBannerAdapter
                    mHomeBanner.start()
                }
            })
    }

    /*
    初始化公告
 */
    private fun initNews(){
        val newsList:Array<News> = arrayOf(
            News("http://q65j2lskn.bkt.clouddn.com/Fg-nTFVoSTWpGkyml9MqHah-_6Co","夏日炎炎，第一波福利还有30秒到达战场"),
            News("http://q65j2lskn.bkt.clouddn.com/FjIWRzzjErAUTSONSIATxhgSzEca","新用户立领1000元优惠券"),
            News("http://q65j2lskn.bkt.clouddn.com/FkiA6CgReWQfHaTEf3ajb7Bzxcla","疫情紧急，大批口罩秒杀")
        )
        //公告
        mNewsFlipperView.setData(newsList)
    }

    //初始化折扣列表
    private fun initDiscount() {
        //列表布局
        val manager = LinearLayoutManager(context)
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mHomeDiscountRv.setLayoutManager(manager)

        //请求数据
        RxRequest.builder()
            .setContext(context!!)
            .setUrl(Constant.DISCOUNT_URL)
            .get()
            .json2List(DiscountData::class.java)
            .subscribe(object :
                RxObserver<List<DiscountData>>(){
                override fun onNext(list: List<DiscountData>) {
                    var retList:MutableList<DiscountData> = list.toMutableList()
                    discountAdapter = DiscountAdapter(retList)
                    mHomeDiscountRv.adapter = discountAdapter
                }
            })
    }

    /*
        初始化主题
     */
    private fun initTopic(){
        RxRequest.builder()
            .setContext(context!!)
            .setUrl(Constant.TOPICS_URL)
            .get()
            .json2List(String::class.java)
            .subscribeBy(
                onNext = {list:List<String>->
                    //绑定数据到viewpager适配器
                    mTopicPager.adapter = TopicAdapter(context!!,
                                                        list)
                    //从第一页开始显示
                    mTopicPager.currentItem = 1
                    //设置VierPager缓存数量
                    //默认为：2，包含左右两页
                    mTopicPager.offscreenPageLimit = 5
                    //实现缩放滑动页面
                    CoverFlow.Builder()
                        .with(mTopicPager)
                        .scale(0.3f)
                        .pagerMargin(-30.0f)
                        .spaceSize(0.0f).build()
                }
            )
    }

    override fun onItemClick(position: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}